<?php


namespace app\api\controller;

use think\Response;
use think\facade\Db;
use think\Request;

class Index extends Wap
{



    public function orderCoupon()
    {
        $order = Db::name('customer_order')->where('order_on', '=', '202303311327284769984214')->select();

        foreach ($order as $i=>$v){
              $this->distributionsss($v['store_id'],$v['pay_price'],1,'ceshi',$v['id']);
        }


    }

    /**
     * 获取小程序参数
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function AppInfo(Request $request)
    {
        if (request()->isPost()) {
            $userInfo = Db::name('mini_user')->where('id', '=', 1)->find();
            $data['store_name'] = $userInfo['store_name'];
            if (isset($userInfo['make_image']) && $userInfo['make_image'] != "") {

                if ($userInfo['make_image'] != "") {
                    $imgsList = unserialize($userInfo['make_image']);
                    foreach ($imgsList as &$val) {
                        $val = get_image_url($val);
                    }
                    $userInfo['make_image'] = $imgsList;
                } else {
                    $userInfo['make_image'] = '';
                }

            } else {
                $userInfo['make_image'] = '';
            }
            $data['make_image'] = $userInfo['make_image'];
            $arr['code'] = 200;
            $arr['data'] = ($data);
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();
        }
    }

    /**
     * 门店信息
     * @param Request $request
     * @return void
     */
    public function index(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();

            $lat = 0;
            if (isset($post['lat'])) {
                $lat = $post['lat'];
            }
            if (isset($post['lng'])) {
                $lag = $post['lng'];
            }
            if (isset($post['pageno'])) {
                $page = $post['pageno']+1;
                $list = Db::name('mini_room_list')->where('mark', 1)->where('is_heidd', '=', 0)->page($page,10)->order('distance asc ')->order('status asc ')->field('id,is_heidd,mobile,room_name,address,province_code,city_code,district_code,latitude,longitude,mark,status,hours_add_time,hours_end_time,head_url,hours_add_time_fen,hours_end_time_fen,(round(6378138 * 2 * asin(sqrt(pow(sin((('.$lat.' * pi()) / 180 - (latitude * pi()) / 180) / 2), 2) + cos(('.$lat.' * pi()) / 180) * cos((latitude * pi()) / 180) * pow(sin(((longitude * pi()) / 180 - ('.$lag.' * pi()) / 180) / 2), 2))))) AS distance')->select();

              if (isset($post['title'])) {

                if ($post['title'] != "") {
                    $list = Db::name('mini_room_list')->where('mark', 1)->where('is_heidd', 0)->where('room_name', 'like', '%' . $post['title'] . '%')->page($page,10)->order('distance asc ')->order('status asc ')->field('id,is_heidd,mobile,room_name,address,province_code,city_code,district_code,latitude,longitude,mark,status,hours_add_time,hours_end_time,head_url,hours_add_time_fen,hours_end_time_fen,(round(6378138 * 2 * asin(sqrt(pow(sin((('.$lat.' * pi()) / 180 - (latitude * pi()) / 180) / 2), 2) + cos(('.$lat.' * pi()) / 180) * cos((latitude * pi()) / 180) * pow(sin(((longitude * pi()) / 180 - ('.$lag.' * pi()) / 180) / 2), 2))))) AS distance')->select();;

                } else {

                    $list = Db::name('mini_room_list')->where('mark', 1)->where('is_heidd', 0)->page($page,10)->order('distance asc ')->order('status asc ')->field('id,is_heidd,room_name,mobile,address,province_code,city_code,district_code,latitude,longitude,mark,status,hours_add_time,hours_end_time,head_url,hours_add_time_fen,hours_end_time_fen,(round(6378138 * 2 * asin(sqrt(pow(sin((('.$lat.' * pi()) / 180 - (latitude * pi()) / 180) / 2), 2) + cos(('.$lat.' * pi()) / 180) * cos((latitude * pi()) / 180) * pow(sin(((longitude * pi()) / 180 - ('.$lag.' * pi()) / 180) / 2), 2))))) AS distance')->select();;

                }
            } elseif (isset($post['newest'])) {

                if ($post['newest'] != "") {
                    $list = Db::name('mini_room_list')->where('mark', 1)->where('is_heidd', '=', 0)->page($page,10)->order('distance asc ')->order('status asc ')->field('id,is_heidd,room_name,mobile,address,province_code,city_code,district_code,latitude,longitude,mark,status,hours_add_time,hours_end_time,head_url,hours_add_time_fen,hours_end_time_fen,(round(6378138 * 2 * asin(sqrt(pow(sin((('.$lat.' * pi()) / 180 - (latitude * pi()) / 180) / 2), 2) + cos(('.$lat.' * pi()) / 180) * cos((latitude * pi()) / 180) * pow(sin(((longitude * pi()) / 180 - ('.$lag.' * pi()) / 180) / 2), 2))))) AS distance')->select();;
                } else {
                    $list = Db::name('mini_room_list')->where('mark', 1)->where('is_heidd', 0)->page($page,10)->order('distance asc ')->order('status asc ')->field('id,is_heidd,room_name,mobile,address,province_code,city_code,district_code,latitude,longitude,mark,status,hours_add_time,hours_end_time,head_url,hours_add_time_fen,hours_end_time_fen,(round(6378138 * 2 * asin(sqrt(pow(sin((('.$lat.' * pi()) / 180 - (latitude * pi()) / 180) / 2), 2) + cos(('.$lat.' * pi()) / 180) * cos((latitude * pi()) / 180) * pow(sin(((longitude * pi()) / 180 - ('.$lag.' * pi()) / 180) / 2), 2))))) AS distance')->select();;
                }

            } elseif (isset($post['city_code'])) {

                if ($post['city_code'] != "") {
                    $list = Db::name('mini_room_list')->where('mark', 1)->where('is_heidd', 0)->where('city_code', $post['city_code'])->page($page,10)->order('distance asc ')->order('status asc ')->field('id,is_heidd,mobile,room_name,address,province_code,city_code,district_code,latitude,longitude,mark,status,hours_add_time,hours_end_time,head_url,hours_add_time_fen,hours_end_time_fen,(round(6378138 * 2 * asin(sqrt(pow(sin((('.$lat.' * pi()) / 180 - (latitude * pi()) / 180) / 2), 2) + cos(('.$lat.' * pi()) / 180) * cos((latitude * pi()) / 180) * pow(sin(((longitude * pi()) / 180 - ('.$lag.' * pi()) / 180) / 2), 2))))) AS distance')->select();;
                } else {

                    $list = Db::name('mini_room_list')->where('mark', 1)->where('is_heidd', 0)->page($page,10)->order('distance asc ')->order('status asc ')->field('id,is_heidd,mobile,room_name,address,province_code,city_code,district_code,latitude,longitude,mark,status,hours_add_time,hours_end_time,head_url,hours_add_time_fen,hours_end_time_fen,(round(6378138 * 2 * asin(sqrt(pow(sin((('.$lat.' * pi()) / 180 - (latitude * pi()) / 180) / 2), 2) + cos(('.$lat.' * pi()) / 180) * cos((latitude * pi()) / 180) * pow(sin(((longitude * pi()) / 180 - ('.$lag.' * pi()) / 180) / 2), 2))))) AS distance')->select();;
                }
            }

           
            }else{
                $list = Db::name('mini_room_list')->where('mark', 1)->where('is_heidd', '=', 0)->order('distance asc ')->order('status asc ')->field('id,is_heidd,mobile,room_name,address,province_code,city_code,district_code,latitude,longitude,mark,status,hours_add_time,hours_end_time,head_url,hours_add_time_fen,hours_end_time_fen,(round(6378138 * 2 * asin(sqrt(pow(sin((('.$lat.' * pi()) / 180 - (latitude * pi()) / 180) / 2), 2) + cos(('.$lat.' * pi()) / 180) * cos((latitude * pi()) / 180) * pow(sin(((longitude * pi()) / 180 - ('.$lag.' * pi()) / 180) / 2), 2))))) AS distance')->select();;

            }



         
            $data['list'] = array();
            if (count($list) > 0) {

                foreach ($list as $i => $v) {
                    $hours_time = $this->time();
                    $hours_add_time = $hours_time['hours_add_time'];
                    $hours_end_time = $hours_time['hours_end_time'];
                    $time_add = $hours_time['time_add'];
                    $distance = "0";

                    if ($v['latitude'] && $v['longitude']) {
                        if ($lat > 0) {
                            $distance = Wap::distance($lat, $lag, $v['latitude'], $v['longitude']);
                            $distance = round($distance, 2);
                        } else {
                            $distance = "未知";
                        }
                    }
                    if ($v['province_code']) {


                        if ($v['province_code']) {
                            $v['province_name'] = Db::name('city')->where('citycode', $v['province_code'])->find();
                        }
                        if ($v['city_code']) {
                            $v['city_name'] = Db::name('city')->where('citycode', $v['city_code'])->find();
                        }
                        if ($v['district_code'] > 0) {
                            $v['district_name'] = Db::name('city')->where('citycode', $v['district_code'])->find();
                            if ($v['district_name']) {
                                if ($v['district_name'] == "") {
                                    $v['district_name']['name'] = ' ';
                                }

                            }
                        } else {
                            $v['district_name']['name'] = ' ';
                        }

                        $v['address_lan'] = $v['province_name']['name'];
                        if (isset($v['province_name']['name'])) {
                            if (isset($v['city_name']['name'])) {
                                $v['address_lan'] = $v['province_name']['name'] . $v['city_name']['name'];

                            }
                            if (isset($v['district_name']['name'])) {
                                $v['address_lan'] = $v['province_name']['name'] . $v['city_name']['name'] . $v['district_name']['name'] . $v['address'];

                            }
                        }

                        $v['city'] = $v['city_name'];
                    }


                    if ($v['head_url'] != "") {
                        $v['head_url'] = get_image_url($v['head_url']);
                    }
                    $data['list'][] = array(
                        'room_id' => $v['id'],
                        'name' => $v['room_name'],
                        'logo_img' => $v['head_url'],
                        'hours' => $hours_add_time[$v['hours_add_time'] + 1] . ':' . $time_add[$v['hours_add_time_fen']] . '-' . $hours_end_time[$v['hours_end_time'] + 1] . ':' . $time_add[$v['hours_end_time_fen']],
                        'distance' =>isset($v['distance']) ?$v['distance']:'0',
                        'mobile' => $v['mobile'],
                        'lat' => $v['latitude'],
                        'lng' => $v['longitude'],
                        'address' => $v['address_lan'],
                        'status' => $v['status'],
                        'is_heidd' => $v['is_heidd'],

                    );
                }
                $sort_arr = $this->sortArrByField($data['list'], 'distance', false);
                if (count($data['list']) > 0) {
                    foreach ($data['list'] as $i => $v) {
                        $v['distance'] = round($v['distance']/1000,2) . '千米';
                        $data['list'][$i] = $v;
                    }
                } else {
                    $data['list'] = array();
                }
            }
            $arr['code'] = 200;
            $arr['data'] = $data;
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();
        }
    }


    /**
     * 数据
     * @param $array
     * @param $field
     * @param false $desc
     */
    public function sortArrByField(&$array, $field, $desc = false)
    {
        $fieldArr = array();
        foreach ($array as $k => $v) {
            $fieldArr[$k] = $v[$field];
        }
        $sort = $desc == false ? SORT_ASC : SORT_DESC;
        array_multisort($fieldArr, $sort, $array);
    }


    /**
     * 城市数据
     * @param Request $request
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function city(Request $request)
    {

        // cache('room', false);
        if (!cache('city')) {
            $data = array();
            $admin = Db::name('mini_room_list')->select();
            foreach ($admin as $i => $v) {
                if ($v['province_code']) {
                    if ($v['province_code']) {
                        $v['province_name'] = Db::name('city')->where('citycode', $v['province_code'])->find();
                    }
                    if ($v['city_code']) {
                        $v['city_name'] = Db::name('city')->where('citycode', $v['city_code'])->find();
                    }
                    if ($v['district_code'] > 0) {
                        $v['district_name'] = Db::name('city')->where('citycode', $v['district_code'])->find();
                    } else {
                        $v['district_name']['name'] = '';
                    }
                } else {
                    $v['province_name']['name'] = '';
                    $v['city_name']['name'] = '';
                }
                $data[] = array(
                    'province_name' => $v['province_name']['name'],
                    'city_name' => $v['city_name']['name'],
                    'province_code' => $v['province_code'],
                    'city_code' => $v['city_code'],
                    'lat' => $v['latitude'],
                    'lng' => $v['longitude'],
                );

            }

            cache('city',$this->array_unset_tt($data));
        }

        $arr['code'] = 200;
        $arr['data'] =  cache('city');
        $arr['message'] = '获取成功';
        Response::create($arr, 'json')->send();
        exit();
    }

    /**
     * 处理重复的数据
     * @param $arr
     * @param $key
     * @return array
     */
    public function array_unset_tt($arr, $key = 'city_name')
    {
        //建立一个目标数组
        $res = array();
        foreach ($arr as $value) {
            //查看有没有重复项
            if (isset($res[$value[$key]])) {
                unset($value[$key]);  //有：销毁
            } else {
                $res[$value[$key]] = $value;
            }
        }
        return $res;
    }


    /**
     * 门店列表
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function room(Request $request)
    {
        if (request()->isPost()) {
            cache('room', false);
            if (!cache('room')) {
                //是否充值
                //          $user = $this->checkToken();
                //           $recharge_order = Db::name('customer_recharge_order')->where('user_id', $user['id'])->where('status', 1)->find();
//            if($recharge_order){
//                $list = Db::name('mini_room_list')->where('id', $recharge_order['store_id'])->where('mark', 1)->order('sort desc')->select();
//            }else{
//                $list = Db::name('mini_room_list')->where('mark', 1)->order('sort desc')->select();
//
//            }
                $list = Db::name('mini_room_list')->where('mark', 1)->where('is_heidd', '=', 0)->order('id desc')->select();
                foreach ($list as $i => $v) {
                    if (isset($v['make_image']) && $v['make_image']) {

                        $imgsList = unserialize($v['make_image']);
                        foreach ($imgsList as &$val) {
                            $val = get_image_url($val);

                        }
                        $v['make_image'] = $imgsList;

                    }

                    if (isset($v['province_code'])) {
                        if ($v['province_code']) {


                            if ($v['province_code']) {
                                $v['province_name'] = Db::name('city')->where('citycode', $v['province_code'])->find();
                            }
                            if ($v['city_code']) {
                                $v['city_name'] = Db::name('city')->where('citycode', $v['city_code'])->find();
                            }
                            if ($v['district_code'] > 0) {
                                $v['district_name'] = Db::name('city')->where('citycode', $v['district_code'])->find();
                                if ($v['district_name']) {
                                    if ($v['district_name'] == "") {
                                        $v['district_name']['name'] = ' ';
                                    }
                                }
                            } else {
                                $v['district_name']['name'] = ' ';
                            }

                            $v['address_lan'] = $v['province_name']['name'];
                            if (isset($v['province_name']['name'])) {
                                if (isset($v['city_name']['name'])) {
                                    $v['address_lan'] = $v['province_name']['name'] . $v['city_name']['name'];

                                }
                                if (isset($v['district_name']['name'])) {
                                    $v['address_lan'] = $v['province_name']['name'] . $v['city_name']['name'] . $v['district_name']['name'] . $v['address'];

                                }
                            }


                        }
                    }
                    $data[] = array(
                        'room_id' => $v['id'],
                        'name' => $v['room_name'],
                        'image_image' => $v['make_image'],
                        'mobile' => $v['mobile'],
                        'address' => $v['address_lan'],
                        'lat' => $v['latitude'],
                        'lng' => $v['longitude'],
                    );
                }

                cache('room', $data);
            }
            $arr['code'] = 200;
            $arr['data'] =  cache('room');
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();
        }
    }



    /**
     * 充值门店列表
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function roomcz(Request $request)
    {
        if (request()->isPost()) {
            //是否充值
            $user = $this->checkToken();
            $recharge_order = Db::name('customer_recharge_order')->where('user_id', $user['id'])->where('status', 1)->find();
            if($recharge_order){
                $list = Db::name('mini_room_list')->where('id', $recharge_order['store_id'])->where('mark', 1)->order('sort desc')->select();
            }else{
                $list = Db::name('mini_room_list')->where('mark', 1)->order('sort desc')->select();

            }

            foreach ($list as $i => $v) {
                if (isset($v['make_image']) && $v['make_image']) {

                    $imgsList = unserialize($v['make_image']);
                    foreach ($imgsList as &$val) {
                        $val = get_image_url($val);

                    }
                    $v['make_image'] = $imgsList;

                }

                if (isset($v['province_code'])) {
                    if ($v['province_code']) {


                        if ($v['province_code']) {
                            $v['province_name'] = Db::name('city')->where('citycode', $v['province_code'])->find();
                        }
                        if ($v['city_code']) {
                            $v['city_name'] = Db::name('city')->where('citycode', $v['city_code'])->find();
                        }
                        if ($v['district_code'] > 0) {
                            $v['district_name'] = Db::name('city')->where('citycode', $v['district_code'])->find();
                            if ($v['district_name']) {
                                if ($v['district_name'] == "") {
                                    $v['district_name']['name'] = ' ';
                                }
                            }
                        } else {
                            $v['district_name']['name'] = ' ';
                        }

                        $v['address_lan'] = $v['province_name']['name'];
                        if (isset($v['province_name']['name'])) {
                            if (isset($v['city_name']['name'])) {
                                $v['address_lan'] = $v['province_name']['name'] . $v['city_name']['name'];

                            }
                            if (isset($v['district_name']['name'])) {
                                $v['address_lan'] = $v['province_name']['name'] . $v['city_name']['name'] . $v['district_name']['name'] . $v['address'];

                            }
                        }


                    }
                }
                $data[] = array(
                    'room_id' => $v['id'],
                    'name' => $v['room_name'],
                    'image_image' => $v['make_image'],
                    'mobile' => $v['mobile'],
                    'address' => $v['address_lan'],
                    'lat' => $v['latitude'],
                    'lng' => $v['longitude'],
                );
            }
            $arr['code'] = 200;
            $arr['data'] = $data;
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();
        }
    }
    /**
     * 省市区数据
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function citydata(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            if (isset($post['pid'])) {
                if ($post['pid'] > 0) {
                    $list = Db::name('city')->where('pid', $post['pid'])->where('mark', 1)->select();
                } else {
                    $list = Db::name('city')->where('level', 1)->where('mark', 1)->select();

                }
            } else {
                $list = Db::name('city')->where('level', 1)->where('mark', 1)->select();

            }
            $data = array();
            foreach ($list as $i => $v) {
                $data[] = array(
                    'name' => $v['name'],
                    'id' => $v['id'],
                    'citycode' => $v['citycode'],

                );
            }
            $arr['code'] = 200;
            $arr['data'] = $data;
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();
        }
    }


    /**
     * 查询邀请码
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function invitation(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            $data = array();
            $info = Db::name('mini_room_list')->where('mobile', $post['mobile'])->find();
            if ($info) {
                $data['invitation_code'] = $info['invitation_code'];
            }

            $arr['code'] = 200;
            $arr['data'] = $data;
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();
        }
    }

    /**
     * 公告
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function notice(Request $request)
    {
        $info = Db::name('notice')->where('status', 1)->order('id desc')->find();

        $data['title'] = isset($info['title']) ? $info['title'] : '';
        $data['content'] = $info['content'];
        $arr['code'] = 200;
        $arr['data'] = $data;
        $arr['message'] = '获取成功';
        Response::create($arr, 'json')->send();
        exit();
    }


}
